﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wicresoft.JDPostBoard.Entity;
using Wicresoft.Common;
using System.Data;
using Wicresoft.Common.MessagesWebService;

public partial class Gift_AllPassList : BaseAuthenticationPage
{
    public string SearchDefaultText
    {
        get
        {
            return "搜索 全部申领";
        }
    }

    public string SearchClause
    {
        get
        {
            if (ViewState["SearchClause"] == null)
                ViewState["SearchClause"] = "1=1 AND IsEnable='True'" + string.Format(" AND ({0} = '{1}' or {0}='{2}')", ApplyLog.SQLCOL_STATUS, Enum.ApplyStatusEnum.审核通过.ToString(), Enum.ApplyStatusEnum.延后审核.ToString());
            return ViewState["SearchClause"].ToString();
        }
        set
        {
            ViewState["SearchClause"] = value;
        }
    }

    protected DataTable DTData
    {
        get
        {
            if (ViewState["DTData"] == null)
                ViewState["DTData"] = new DataTable();
            return ViewState["DTData"] as DataTable;
        }
        set
        {
            ViewState["DTData"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Wicresoft.Framework.Organization.OrganizationPublicHelper.IsUserInRole(CurrentUser.ID, new Guid(System.Configuration.ConfigurationManager.AppSettings["OfficeAdminID"].ToLower())))
        {
            Response.Write("对不起，您无权查看此页！<br /><br />");
            Response.Write("您可以查看<a href='MyApplyList.aspx'>本人申领页面</a>");
            Response.End();
        }

        this.Title = "领取列表";
        this.tbSearch.Attributes.Add("onfocus", string.Format("clearText(this, '{0}');", SearchDefaultText));
        this.tbSearch.Attributes.Add("onblur", string.Format("insertText(this, '{0}');", SearchDefaultText));
        this.tbSearch.Attributes.Add("onkeydown", "return searchClick();");


        if (!IsPostBack)
        {
            tbSearch.Text = SearchDefaultText;
            this.Order.OrderColumn = "ApplyTime";
            this.Order.OrderDirection = "DESC";

            this.ddlTime.Items.Add("申请时间");
            this.ddlTime.Items.Add("审核时间");
            this.ddlTime.Items.Insert(0, new ListItem("--请选择--", string.Empty));

            this.cbPass.Checked = true;
            this.cbyhsh.Checked = true;
            BindData();
        }

    }

    protected override void Pager_PageChanged(object sender, EventArgs e)
    {
        BindData();
        base.Pager_PageChanged(sender, e);
    }

    protected override void Order_OrderChanged(object sender, EventArgs e)
    {
        BindData();
        base.Order_OrderChanged(sender, e);
    }

    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        BindData();
    }

    private void BindData()
    {
        int recordCount = 0;
        DTData = DataAccess.Select(typeof(ApplyLog),Pager.PageIndex, Pager.PageSize, SearchClause, string.Format("{0} {1}", this.Order.OrderColumn, this.Order.OrderDirection), ref recordCount);
        Pager.RecordCount = recordCount;
        rptOrder.DataSource = DTData;
        rptOrder.DataBind();
    }

    protected void rptProduct_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "Arrived":

                ApplyLog ol = new ApplyLog(new Guid(e.CommandArgument.ToString()));
                ol.Load();
                if (ol.Status == Enum.ApplyStatusEnum.审核通过.ToString())
                {
                    ol.Status = Enum.ApplyStatusEnum.已到货.ToString();
                    ol.ArrivalTime = DateTime.Now;
                    if (ol.Save())
                    {
                        string MessageTitle = "到货通知";
                        string MessageText = "您的申请“" + ol.ApplyTitle + "”的已经到货，请及时领取！";
                        string MessageModuleID = WebCommon.GetConfig("MessageModuleID");
                        MessagesService ms = new MessagesService();
                        string s = ms.SendMessageByModuleID(MessageModuleID, CurrentUser.Name, ol.ApplierName, MessageTitle, MessageText, false, 0, "", DateTime.Now.ToString());
                    }
                }
                BindData();
                break;

        }
    }

    protected void rptProduct_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (tbSearch.Text != string.Empty && tbSearch.Text != SearchDefaultText)
            SearchClause = string.Format("{0} LIKE N'%{1}%' OR {2} LIKE N'%{1}%'", ApplyLog.SQLCOL_APPLYTITLE, tbSearch.Text.Replace("'", "''").Replace("%", "[%]"), ApplyLog.SQLCOL_APPLIERDEPARTMENT);
        else
            SearchClause = "1=1";
        //Pager.ResetPager();
        SearchClause += string.Format(" AND IsEnable='True' AND {0} = '{1}'", ApplyLog.SQLCOL_STATUS, Enum.ApplyStatusEnum.审核通过.ToString());

        BindData();

    }

    /*protected void AllNotice_Click(object sender, EventArgs e)
    {

    }

    protected void AllArrival_Click(object sender, EventArgs e)
    {
        foreach (DataRow dr in DTData.Rows)
        {
            OrderList ol = new OrderList(int.Parse(dr["ID"].ToString()));
            ol.Load();
            ol.Status = "已到货";
            ol.ArrivalTime = Convert.ToDateTime(DateTime.Now.ToString());
            ol.Save();
        }
        BindData();
    }*/

    protected void btnAdvancedSearch_Click(object sender, EventArgs e)
    {
        SearchClause = "1=1 AND IsEnable='True'";
       
        string TempSearch = null;
        if (tbTitle.Text != string.Empty)
            SearchClause += string.Format(" AND {0} LIKE N'%{1}%'", ApplyLog.SQLCOL_APPLYTITLE, tbTitle.Text.Replace("'", "''").Replace("%", "[%]"));

        if (this.tbStartTime.Text != "" && this.tbEndTime.Text != "")
        {
            if (Convert.ToDateTime(this.tbStartTime.Text) > Convert.ToDateTime(this.tbEndTime.Text))
            {
                Response.Write("<script>alert('开始日期不能小于结束日期');</script>");
                return;
            }
            switch (ddlTime.Text)
            {
                case "申请时间":
                    SearchClause += string.Format(" AND {0} > '{1}' AND {0} < '{2}'", ApplyLog.SQLCOL_APPLYTIME, tbStartTime.Text, tbEndTime.Text);
                    break;
                case "审核时间":
                    SearchClause += string.Format(" AND {0} > '{1}' AND {0} < '{2}'", ApplyLog.SQLCOL_DEALTIME, tbStartTime.Text, tbEndTime.Text);
                    break;
                case "到货时间":
                    SearchClause += string.Format(" AND {0} > '{1}' AND {0} < '{2}'", ApplyLog.SQLCOL_ARRIVALTIME, tbStartTime.Text, tbEndTime.Text);
                    break;
                case "确认时间":
                    SearchClause += string.Format(" AND {0} > '{1}' AND {0} < '{2}'", ApplyLog.SQLCOL_CONFIRMTIME, tbStartTime.Text, tbEndTime.Text);
                    break;
                case "":
                    Response.Write("<script>alert('请选择查询时间类型')</script>");
                    break;
                default:
                    break;
            }
        }
        else if (this.tbStartTime.Text != "")
        {
            switch (ddlTime.Text)
            {
                case "申请时间":
                    SearchClause += string.Format(" AND {0} > '{1}'", ApplyLog.SQLCOL_APPLYTIME, tbStartTime.Text);
                    break;
                case "审核时间":
                    SearchClause += string.Format(" AND {0} > '{1}'", ApplyLog.SQLCOL_DEALTIME, tbStartTime.Text);
                    break;
                case "到货时间":
                    SearchClause += string.Format(" AND {0} > '{1}'", ApplyLog.SQLCOL_ARRIVALTIME, tbStartTime.Text);
                    break;
                case "确认时间":
                    SearchClause += string.Format(" AND {0} > '{1}'", ApplyLog.SQLCOL_CONFIRMTIME, tbStartTime.Text);
                    break;
                case "":
                    Response.Write("<script>alert('请选择查询时间类型')</script>");
                    break;
                default:
                    break;
            }
        }
        else if (this.tbEndTime.Text != "")
        {
            switch (ddlTime.Text)
            {
                case "申请时间":
                    SearchClause += string.Format(" AND {0} < '{1}'", ApplyLog.SQLCOL_APPLYTIME, tbEndTime.Text);
                    break;
                case "审核时间":
                    SearchClause += string.Format(" AND {0} < '{1}'", ApplyLog.SQLCOL_DEALTIME, tbEndTime.Text);
                    break;
                case "到货时间":
                    SearchClause += string.Format(" AND {0} < '{1}'", ApplyLog.SQLCOL_ARRIVALTIME, tbEndTime.Text);
                    break;
                case "确认时间":
                    SearchClause += string.Format(" AND {0} < '{1}'", ApplyLog.SQLCOL_CONFIRMTIME, tbEndTime.Text);
                    break;
                case "":
                    Response.Write("<script>alert('请选择查询时间类型')</script>");
                    break;
                default:
                    break;
            }
        }
        else
        { }

        TempSearch = "(1=1";
        if (cbSubmit.Checked)
        {

            TempSearch += string.Format(" or {0} = '{1}'", ApplyLog.SQLCOL_STATUS, cbSubmit.Text);

        }
        if (cbPass.Checked)
        {
            TempSearch += string.Format(" or {0} = '{1}'", ApplyLog.SQLCOL_STATUS, cbPass.Text);
           
        }
        if (cbNotPass.Checked)
        {

            TempSearch += string.Format(" or {0} = '{1}'", ApplyLog.SQLCOL_STATUS, cbNotPass.Text);
        }
        if (cbArrival.Checked)
        {
            TempSearch += string.Format(" or {0} = '{1}' or {0}='{2}'", ApplyLog.SQLCOL_STATUS, cbArrival.Text,"延后审核已发货");
            
        }
        if (cbConfirm.Checked)
        {
            TempSearch += string.Format(" or {0} = '{1}'", ApplyLog.SQLCOL_STATUS, cbConfirm.Text);
             
        }
        if (cbyhsh.Checked)
        {
            TempSearch += string.Format(" or {0} = '{1}'", ApplyLog.SQLCOL_STATUS, cbyhsh.Text);
           
 
        }
        if (cbyhshyfh.Checked)
        {
            TempSearch += string.Format(" or {0} = '{1}'", ApplyLog.SQLCOL_STATUS, cbyhshyfh.Text);
            
 
        }
        TempSearch += ")";
        TempSearch = TempSearch.Replace("(1=1 or", "(").Replace("(1=1)", "");

      
        if (TempSearch!=string.Empty)
            SearchClause +=" and "+ TempSearch.ToString();
      
        
        //Pager.ResetPager();


        BindData();
    }
}
